package org.ims.inv.web.action.project;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.ims.inv.model.project.ModelProject;
import org.ims.inv.model.system.ModelAppUser;

import cn.trymore.core.util.UtilString;
import cn.trymore.core.web.paging.PaginationSupport;
import cn.trymore.core.web.paging.PagingBean;

/**
 * Action: 项目分配
 * 
 * @author Jeccy.Zhao
 *
 */
public class ProjectAllocateAction
extends ProjectBaseAction
{
	/**
	 * The LOGGER
	 */
	private static final Logger LOGGER = Logger.getLogger(ProjectAllocateAction.class);
	
	/**
	 * <b>[WebAction]</b> 
	 * <br/>
	 * Allocate Index Page
	 */
	public ActionForward pageProjectAllocateIndex (ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
	{
		try
		{
			// 获取 "未分配" 数
			request.setAttribute("numProjectForAllocate", this.getNumProjectForAllocate());
			
			// 获取项目所属行业
			request.setAttribute("dictInds", this.getDictionariesByType("DICT_PRO_INDUSTRY"));
			
			this.outWriteAllParameters(request);
			
			return mapping.findForward("page.project.allocate.index");
		}
		catch (Exception e)
		{
			LOGGER.error("Exception raised when loading page", e);
			return ajaxPrint(response, getErrorCallback("数据加载失败,原因:" + e.getMessage()));
		}
	}
	
	/**
	 * <b>[WebAction]</b> 
	 * <br/>
	 * Loads Project Allocation Data
	 */
	public ActionForward actionLoadProjectAllocate (ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
	{
		try
		{
			ModelProject formEntity = (ModelProject) form;
			
			// 项目负责人
			String projectManagerName = request.getParameter("mgr.username");
			if (UtilString.isNotEmpty(projectManagerName))
			{
				projectManagerName = new String(
						projectManagerName.getBytes("ISO-8859-1"), "UTF-8");
				
				ModelAppUser manager = new ModelAppUser();
				manager.setUsername(projectManagerName);
				formEntity.setProjectManager(manager);
			}
			
			// 项目名称
			if (UtilString.isNotEmpty(formEntity.getProjectName()))
			{
				formEntity.setProjectName(new String(
						formEntity.getProjectName().getBytes("ISO-8859-1"), "UTF-8"));
			}
			
			// 行业类别
			String projectTypeId = request.getParameter("projectTypeId");
			if (UtilString.isNotEmpty(projectTypeId))
			{
				formEntity.setProjectType(this.serviceAppDictionary.get(projectTypeId));
			}
			
			PagingBean pagingBean = this.getPagingBean(request);
			PaginationSupport<ModelProject> pagingData = 
				this.serviceProject.getUnAppropriatedProjects(formEntity, pagingBean);
			
			request.setAttribute("datagrid", pagingData);
			request.setAttribute("formEntity", formEntity);
			
			this.outWritePagination(request, pagingBean, pagingData);
			this.outWriteAllParameters(request);
			
			// 设置模块标记, 主要用于展现不同的页面操作
			request.setAttribute("module", "allocate");
			
			return mapping.findForward("base.project.datagrid");
		}
		catch (Exception e)
		{
			LOGGER.error("Exception raised when loading user dialog page", e);
			return ajaxPrint(response, getErrorCallback("数据加载失败,原因:" + e.getMessage()));
		}
	}
	
	public static Logger getLogger()
	{
		return LOGGER;
	}
	
}
